/*   
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * DMGIS. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with DMGIS.   
 *   
 */
package com.dmgis.qxfw.service;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.bson.types.Binary;
import org.springframework.stereotype.Service;

import com.dmgis.qxfw.common.DbConfig;
import com.dmgis.qxfw.utils.MongoDBUtil;
//import com.mongodb.MongoClient;
//import com.mongodb.client.MongoCollection;
//import com.mongodb.client.MongoCursor;

import java.util.Map;

@Service("MongoService")
public class MongoService implements IMongoService {

	@Override
	public byte[] selectData(String collectionName, Map<String, Object> queryMap) {
		// TODO Auto-generated method stub
		byte[] result = null;
		// 无用户密码验证实例
		MongoClient client = MongoDBUtil.instance().getMongoConnect(DbConfig.HOST, DbConfig.PORT);
		// 需要用户密码实例
		MongoCollection<Document> documentMongoCollection = MongoDBUtil.instance().getMongoCollection(client,
				DbConfig.MONGDB_NAME, collectionName);
		// 插入文档
		MongoCursor<Document> documents = MongoDBUtil.instance().queryDocument(documentMongoCollection, queryMap);
		while (documents.hasNext()) {
			Document document = documents.next();
			Object obj = document.get("data");
			if (obj instanceof Binary) {
				Binary binary = (Binary) obj;
				// 拿到字节数组
				byte[] filebyte = binary.getData();
				result = filebyte;
				// byte type = binary.getType();
				// FileUtil.bytesToFile(filebyte, "D:\\temp\\2.doc");
			}
		}
		client.close();
		return result;
	}

	@Override
	public int insertData(String collectionName, Map<String, Object> insertMap) {
		// TODO Auto-generated method stub
		try {
//			MongoClient client = MongoDBUtil.instance().getMongoConnect(DbConfig.HOST, DbConfig.PORT);
			MongoClient client = MongoClients.create("mongodb://"+DbConfig.HOST+":"+DbConfig.PORT);
			// 需要用户密码实例
			MongoCollection<Document> documentMongoCollection = MongoDBUtil.instance().getMongoCollection(client,DbConfig.MONGDB_NAME, collectionName);
			// 插入文档
			MongoDBUtil.instance().insertDoucument(documentMongoCollection, insertMap);
			client.close();
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public long deleteData(String collectionName, Map<String, Object> deleteMap) {
		// TODO Auto-generated method stub
		long count = 0;
		// 删除文档数据
		try {
			MongoClient client = MongoDBUtil.instance().getMongoConnect(DbConfig.HOST, DbConfig.PORT);
			// 需要用户密码实例
			MongoCollection<Document> documentMongoCollection = MongoDBUtil.instance().getMongoCollection(client,
					DbConfig.MONGDB_NAME, collectionName);
			count = MongoDBUtil.instance().deleteDocument(documentMongoCollection, true, deleteMap);
			client.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			count = -1;
		}
		return count;
	}

	@Override
	public long updateData(String collectionName, Map<String, Object> updateMap, Map<String, Object> updateData) {
		// TODO Auto-generated method stub
		long count = 0;
		try {
			MongoClient client = MongoDBUtil.instance().getMongoConnect(DbConfig.HOST, DbConfig.PORT);
			// 需要用户密码实例
			MongoCollection<Document> documentMongoCollection = MongoDBUtil.instance().getMongoCollection(client,
					DbConfig.MONGDB_NAME, collectionName);
			count = MongoDBUtil.instance().updateDocument(documentMongoCollection, updateMap, updateData);
			client.close();
		} catch (Exception e) {
			// TODO: handle exception
			count = -1;
		}
		return count;
	}

	@Override
	public long existData(String collectionName, Map<String, Object> existMap) {
		// TODO Auto-generated method stub
		long count = 0;
		// 删除文档数据
		try {
			MongoClient client = MongoDBUtil.instance().getMongoConnect(DbConfig.HOST, DbConfig.PORT);
			// 需要用户密码实例
			MongoCollection<Document> documentMongoCollection = MongoDBUtil.instance().getMongoCollection(client,
					DbConfig.MONGDB_NAME, collectionName);
			count = MongoDBUtil.instance().countTable(documentMongoCollection, existMap);
			client.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			count = -1;
		}
		return count;
	}

}
